package com.krazevina.kar;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.rms.RecordStore;

public final class Bookmark extends Form{
    List lv;
    Button bcall, bookmark, addressbook, setting;
    
    Image fav,nofav;
    Dialog menu;
    Label title;
    static boolean headerSearch = false;
    static Image backgr;
    static TaxiResource taxr;
    
    public Bookmark() {
        getStyle().setBgTransparency(255);
        lv = new List();
        lv.setRenderer(new ListCellRender());
        lv.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(lv.getSelectedIndex()>=0){
                    if(m==null)m = new MenuDialog();
                    m.show();
                }
            }
        });
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addView();
        taxr = new TaxiResource();
        try{
            taxr.init();
        }catch(Exception e){}
        getCall();
    }

    void addView(){
        title = new Label("Số đã lưu");
        title.setPreferredW(getWidth());
        title.setPreferredH(40);
        title.setEnabled(false);
        title.getDisabledStyle().setAlignment(Container.CENTER);
        title.getDisabledStyle().setBorder(null);
        title.setFocusable(false);
        title.getDisabledStyle().setMargin(0,0,0,0);
        title.getDisabledStyle().setPadding(0,0,0,0);
        title.getDisabledStyle().setFgColor(0xffffff);
        try {
            title.getDisabledStyle().setBgImage(Image.createImage("/top.png"));
            addComponent(title);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        lv.setPreferredH(getHeight()-89);
        lv.setPreferredW(getWidth());
        addComponent(lv);
        Container con = new Container(new BoxLayout(BoxLayout.X_AXIS));
        con.getStyle().setBgTransparency(255);
        try {
            con.getStyle().setBgImage(Image.createImage("/bot.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        con.setPreferredW(getWidth());
        con.setPreferredH(49);
        
        try {
            bcall = new Button(Image.createImage("/phoneicon.png"));
            addressbook = new Button(Image.createImage("/addrbook.png"));
            bookmark = new Button(Image.createImage("/bookmarkiconactiv.png"));
            setting = new Button(Image.createImage("/setting.png"));
            con.addComponent(bcall);
            con.addComponent(addressbook);
            con.addComponent(bookmark);
            con.addComponent(setting);
            settran(bcall, Image.createImage("/phoneiconactiv.png"));
            settran(addressbook, Image.createImage("/addrbookactiv.png"));
            settran(bookmark, Image.createImage("/bookmarkiconactiv.png"));
            settran(setting, Image.createImage("/settingactiv.png"));
            addressbook.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    Main.getInstance().showAddr();
                }
            });
            bcall.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    Main.getInstance().showMain();
                }
            });
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        addComponent(con);
    }
    void settran(Button b,Image i){
        Style s = new Style();
        s.setBgTransparency(0);
        s.setMargin(0,0,0,0);
        s.setPadding(0,0,0,0);
        s.setBorder(null);
        Style s1 = new Style();
        s1.setBgTransparency(0);
        s1.setMargin(0,0,0,0);
        s1.setPadding(0,0,0,0);
        s1.setBorder(Border.createLineBorder(1, 0xffffff));
        b.setUnselectedStyle(s);
        b.setSelectedStyle(s1);
        b.setPressedStyle(s);
        b.setPressedIcon(i);
    }
    int page = 0;Label searching;
    Image imfav;
    void addTx(Taxi t){
        Label lblnum,lblname;
        Container out = new Container(new BoxLayout(BoxLayout.X_AXIS));
        Container con = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        lblnum = new Label(t.num);
        lblname = new Label(t.name);
        lblnum.setEnabled(false);
        Label lbl = new Label();
        lbl.setEnabled(false);
        lbl.setPreferredW(getWidth()-60);
        lbl.setPreferredH(1);
        lbl.getDisabledStyle().setBgTransparency(255);
        lbl.getDisabledStyle().setBgColor(0xeeeeee);
        lblnum.getDisabledStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
        con.setPreferredW(getWidth()-60);
        con.setPreferredH(70);
        
        con.addComponent(lblnum);
        con.addComponent(lblname);
        con.addComponent(lbl);
        
        out.setPreferredH(70);
        out.setPreferredW(getWidth());
        if(imfav==null)try {
            imfav = Image.createImage("/fav.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        Label lbfav = new Label();
        lbfav.setEnabled(false);
        lbfav.getDisabledStyle().setBgImage(imfav);
        if(t.bookmark)lbfav.setVisible(true);
        else lbfav.setVisible(false);
        lbfav.setPreferredH(60);
        lbfav.setPreferredW(60);
        out.addComponent(con);
        out.addComponent(lbfav);
        t.lbfav = lbfav;
        lv.addItem(out);
    }
    
    void call(String num){
        Main.getInstance().call(num);
    }
    
    static Vector call;
    
    void getCall(){
        String s,s1[];
        try {
            call = new Vector();
            Prov prov;
            for (int x = 0; x < taxr.prov.size(); x++){
                prov = (Prov)taxr.prov.elementAt(x);
                for(int y =0;y<prov.taxi.size();y++){
                    if(((Taxi)prov.taxi.elementAt(y)).bookmark)
                    {
                        call.addElement(prov.taxi.elementAt(y));
                        addTx(((Taxi)prov.taxi.elementAt(y)));
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }
    
    Command exit;
    void addCmd(){
        try {
            exit = new Command("Thoát");
            removeAllCommands();
            addCommand(exit);
            getSoftButton(0).setPreferredH(30);
            getSoftButton(1).setPreferredH(30);
            repaint();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void show() {
        super.show();
        setFocused(bookmark);
    }
 
    protected void actionCommand(Command cmd) {
        super.actionCommand(cmd);
//        if(cmd.equals(com)){
//            if(m==null) m = new MenuDialog();
//            m.show(60,0,0,40,true,false);
//        }else{
            Main.getInstance().notifyDestroyed();
//        }
    }
    
    Button bookmarkbot,callbot,cancel;
    MenuDialog m;
    int []favlist;
    Command back,emp;
    int contextFocus = 0;
    
    class MenuDialog extends Dialog{
        public MenuDialog() {
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            getStyle().setAlignment(Component.CENTER);
            bookmarkbot = new Button("Lưu/Bỏ lưu");
            callbot = new Button("Gọi");
            S.setTrans(bookmarkbot);S.setTrans(callbot);
            S.setTrans(cancel);
            try{
                back = new Command("Quay lại");
            }catch(Exception e){}
            addComponent(bookmarkbot);
            addComponent(callbot);
            addComponent(cancel);
            addCommand(back);
            Style s = new Style();
            s.setMargin(0,0,0,0);
            s.setPadding(0,0,0,0);
            s.setBorder(null);
            getSoftButton(0).setPreferredH(40);
            getSoftButton(1).setPreferredH(40);
            bookmark.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    try {
                        Taxi cur = ((Taxi)call.elementAt(lv.getSelectedIndex()));
                        if(cur.bookmark){
                            cur.unBookmark();
                            cur.lbfav.setVisible(false);
                        }else{
                            cur.bookmark();
                            cur.lbfav.setVisible(true);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    } 
                    m.dispose();
                }
            });
            callbot.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                    Taxi t = ((Taxi)call.elementAt(lv.getSelectedIndex()));
                    call(t);
                }
            });
            cancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    m.dispose();
                }
            });
        }
    }
    void call(Taxi t){
        Main.getInstance().call(t.num);
        MainForm.setCall(t);
    }
}
